﻿namespace CL_Searcher
{
    using System;
    using System.Net;
    using System.Net.Mail;
    using System.Windows;

    /// <summary>
    /// Interaction logic for SettingsWindow.xaml
    /// </summary>
    public partial class SettingsWindow : Window
    {
        public SettingsWindow()
        {
            InitializeComponent();
        }

        private void Btn_TestEmail_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                int port;
                bool result = int.TryParse(txt_EmailPort.Text, out port);
                if (!result)
                {
                    MessageBox.Show("The Email Port has to be an integer.", "Error");
                    return;
                }

                SmtpClient client = new SmtpClient();
                client.Host = txt_EmailServer.Text;
                client.Port = port;
                client.UseDefaultCredentials = false;
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.EnableSsl = (bool)ckb_EmailUseSSL.IsChecked;
                client.Credentials = new NetworkCredential(txt_EmailUsername.Text, txt_EmailPassword.Text);
                MailMessage mail = new MailMessage(txt_EmailFromAddress.Text, txt_EmailToAddress.Text);
                mail.Subject = "CL Alert: " + "Test";
                mail.Body = "Test email";
                client.Send(mail);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
